package com.lindroy.androidplayer.base

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.view.MenuItem
import androidx.annotation.ColorRes
import androidx.annotation.StringRes
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import com.lindroid.androidutilskt.statics.AppManager
import com.lindroy.androidplayer.R
import com.lindroy.androidplayer.base.viewmodel.BaseViewModel
import com.lindroy.androidplayer.extensions.isNotNull
import com.lindroy.androidplayer.ui.dialog.LoadingDialog
import com.lindroy.lib_utils.extensions.getResString
import kotlinx.android.synthetic.main.toolbar.view.*

/**
 * @author Lin
 * @date 2021/4/7
 * @function 基类Activity
 */
abstract class BaseActivity : AppCompatActivity() {
    protected lateinit var mContext: Context

    private val loadingDialog: LoadingDialog by lazy { LoadingDialog.build(this) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(layoutId)
        mContext = this
        AppManager.addActivity(this)
        initView()
        initData()
        initListener()
        initClick()
    }

    fun initToolBar(
        title: String = getString(R.string.app_name),
        isShowArrow: Boolean = true,
        @ColorRes toolBarColor: Int = R.color.primary_dark,
        onBack: ((toolbar: Toolbar) -> Unit)? = null
    ) {
        val toolView = window.decorView
        toolView.toolBar.apply {
            this.title = title
            this.setBackgroundColor(ContextCompat.getColor(mContext, toolBarColor))
            //ToolBar的属性设置要在setSupportActionBar方法之前调用
            setSupportActionBar(this)
            supportActionBar?.setDisplayHomeAsUpEnabled(isShowArrow)
            this.setNavigationOnClickListener {
                if(onBack.isNotNull()){
                    onBack?.invoke(this)
                }else{
                    //如果子类页面没有处理
                    finish()
                }
            }
        }
    }

    fun initToolBar(
        @StringRes stringId: Int,
        isShowArrow: Boolean = true,
        onBack: ((toolbar: Toolbar) -> Unit)? = null
    ) {
        initToolBar(mContext.getString(stringId), isShowArrow, onBack = onBack)
    }

    fun setToolBarTitle(@StringRes stringId: Int) {
        window.decorView.toolBar.title = getResString(stringId)
    }

    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        when (item!!.itemId) {
            //为了兼容WebView页面的二级页面回退时间，不能在这里处理返回按钮的点击事件
//            android.R.id.home -> finish()
        }
        return true
    }


    override fun onDestroy() {
        super.onDestroy()
        AppManager.removeActivity(this)
    }

    override fun onStop() {
        super.onStop()
    }

    fun showLoading(msg: String = getResString(R.string.loading)) {
        loadingDialog.show(msg)
    }

    fun dismissLoading() {
        Handler().postDelayed({
            loadingDialog.dismiss()
        }, 500)
    }

    protected fun addLoadingObserve(vararg viewModels: BaseViewModel) {
        viewModels.forEach { viewModel ->
            viewModel.loadingChange.showDialog.observeInActivity(this) {
                showLoading(it)
            }
            viewModel.loadingChange.dismissDialog.observeInActivity(this) {
                dismissLoading()
            }
        }
    }


    abstract val layoutId: Int
    abstract fun initView()
    abstract fun initData()
    abstract fun initListener()
    abstract fun initClick()
//    open fun isRegisterEvent(): Boolean = false
}